#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
    int sum = 0;
public:
    int sumNumbers(TreeNode* root) {
        dfs(root, 0);
        return sum;

    }
    void dfs(TreeNode* root, int pre)
    {
        if (root == nullptr) return;
        pre = pre * 10 + root->val;
        if (root->left == nullptr && root->right == nullptr)
        {
            sum += pre;
            return;
        }
        dfs(root->left, pre);
        dfs(root->right, pre);
        return;
    }

};